/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseResourcePermissionDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseResourcePermissionDao : BaseDao, IBaseDao
    {
        private static BaseResourcePermissionDao instance = null;
        private static object locker = new object();

        public BaseResourcePermissionDao()
        {
            base.CurrentTableName = BaseResourcePermissionTable.TableName;
        }

        public BaseResourcePermissionDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseResourcePermissionDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseResourcePermissionDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseResourcePermissionEntity resourcePermissionEntity)
        {
            string str = string.Empty;
            if (!this.PermissionExists(resourcePermissionEntity.PermissionID, resourcePermissionEntity.ResourceCategory, resourcePermissionEntity.ResourceID))
            {
                str = this.AddEntity(resourcePermissionEntity);
            }
            return str;
        }

        public string AddEntity(BaseResourcePermissionEntity resourcePermissionEntity)
        {
            string iD = resourcePermissionEntity.ID;
            if (string.IsNullOrEmpty(iD))
            {
                iD = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseResourcePermissionTable.TableName);
            }
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseResourcePermissionTable.TableName);
            sqlBuilder.SetValue(BaseResourcePermissionTable.FieldID, iD);
            this.SetEntity(sqlBuilder, resourcePermissionEntity);
            sqlBuilder.SetValue(BaseResourcePermissionTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseResourcePermissionTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return iD;
        }

        public bool CheckPermission(BaseUserInfo userInfo, string permissionCode)
        {
            BasePermissionAdminDao dao = new BasePermissionAdminDao(base.DbHelper, base.UserInfo);
            string str = dao.GetProperty(BasePermissionTable.FieldCode, permissionCode, BasePermissionTable.FieldID);
            if (string.IsNullOrEmpty(str))
            {
                BasePermissionEntity permissionEntity = new BasePermissionEntity
                {
                    Code = permissionCode,
                    FullName = permissionCode,
                    ParentID = null,
                    IsScope = false,
                    AllowDelete = true,
                    AllowEdit = true,
                    Enabled = true
                };
                dao.AddEntity(permissionEntity);
            }
            else
            {
                SQLBuilder builder = new SQLBuilder(base.DbHelper);
                builder.BeginUpdate(BasePermissionTable.TableName);
                builder.SetDBNow(BasePermissionTable.FieldLastCall);
                builder.SetWhere(BasePermissionTable.FieldID, str);
                builder.EndUpdate();
            }
            return (userInfo.IsAdministrator || this.CheckPermissionByUser(userInfo.ID, permissionCode));
        }

        public bool CheckPermission(IDbHelper dbHelper, BaseUserInfo userInfo, string permissionCode)
        {
            base.DbHelper = dbHelper;
            base.UserInfo = userInfo;
            return this.CheckPermission(userInfo, permissionCode);
        }

        public bool CheckPermissionByUser(string userID, string permissionCode)
        {
            string iD = new BasePermissionAdminDao(base.DbHelper).GetID(permissionCode);
            if (string.IsNullOrEmpty(iD))
            {
                return false;
            }
            return (this.CheckUserPermission(base.DbHelper, userID, iD) || this.CheckRolePermission(base.DbHelper, userID, iD));
        }

        public bool CheckPermissionByUser(IDbHelper dbHelper, string userID, string permissionCode)
        {
            base.DbHelper = dbHelper;
            return this.CheckPermissionByUser(userID, permissionCode);
        }

        private bool CheckResourcePermission(IDbHelper dbHelper, string resourceCategory, string resourceID, string permissionID)
        {
            string sqlQuery = " SELECT COUNT(*)    FROM " + BaseResourcePermissionTable.TableName + "  WHERE (" + BaseResourcePermissionTable.FieldResourceCategory + " = '" + resourceCategory + "')         AND (" + BaseResourcePermissionTable.FieldEnabled + " = 1)         AND (" + BaseResourcePermissionTable.FieldResourceID + " = '" + resourceID + "')         AND (" + BaseResourcePermissionTable.FieldPermissionID + " = '" + permissionID + "')";
            int num = 0;
            object obj2 = dbHelper.ExecuteScalar(sqlQuery);
            if (obj2 != null)
            {
                num = int.Parse(obj2.ToString());
            }
            return (num > 0);
        }

        private bool CheckRolePermission(IDbHelper dbHelper, string userID, string permissionID)
        {
            string sqlQuery = " SELECT COUNT(*)    FROM " + BaseResourcePermissionTable.TableName + "  WHERE (" + BaseResourcePermissionTable.FieldResourceCategory + " = 'Role')         AND (" + BaseResourcePermissionTable.FieldEnabled + " = 1)         AND (" + BaseResourcePermissionTable.FieldResourceID + " IN (  SELECT " + BaseUserRoleTable.FieldRoleID + "   FROM " + BaseUserRoleTable.TableName + "  WHERE " + BaseUserRoleTable.FieldUserID + " = '" + userID + "'         AND " + BaseUserRoleTable.FieldEnabled + " = 1   UNION  SELECT " + BaseUserTable.FieldRole + "   FROM " + BaseUserTable.TableName + "  WHERE " + BaseUserTable.FieldID + " = '" + userID + "') )         AND (" + BaseResourcePermissionTable.FieldPermissionID + " = '" + permissionID + "') ";
            int num = 0;
            object obj2 = dbHelper.ExecuteScalar(sqlQuery);
            if (obj2 != null)
            {
                num = int.Parse(obj2.ToString());
            }
            return (num > 0);
        }

        private bool CheckUserPermission(IDbHelper dbHelper, string userID, string permissionID)
        {
            return this.CheckResourcePermission(dbHelper, "User", userID, permissionID);
        }

        public DataTable GetPermission(IDbHelper dbHelper, BaseUserInfo userInfo)
        {
            BasePermissionAdminDao dao = new BasePermissionAdminDao(dbHelper, userInfo);
            if (userInfo.IsAdministrator)
            {
                return dao.GetDT(BasePermissionTable.FieldEnabled, "1", BasePermissionTable.FieldSortCode);
            }
            return this.GetPermissionByUser(dbHelper, userInfo.ID);
        }

        public DataTable GetPermissionByUser(IDbHelper dbHelper, string userID)
        {
            string sqlQuery = " SELECT *    FROM " + BasePermissionTable.TableName + "  WHERE " + BasePermissionTable.FieldEnabled + " = 1         AND " + BasePermissionTable.FieldID + " IN (  SELECT " + BaseResourcePermissionTable.FieldPermissionID + "   FROM " + BaseResourcePermissionTable.TableName + "  WHERE (" + BaseResourcePermissionTable.FieldResourceCategory + " = 'User')         AND (" + BaseResourcePermissionTable.FieldEnabled + " = 1)         AND (" + BaseResourcePermissionTable.FieldResourceID + " = '" + userID + "') UNION  SELECT " + BaseResourcePermissionTable.FieldPermissionID + "   FROM " + BaseResourcePermissionTable.TableName + "  WHERE (" + BaseResourcePermissionTable.FieldResourceCategory + " = 'Role')         AND (" + BaseResourcePermissionTable.FieldEnabled + " = 1)         AND (" + BaseResourcePermissionTable.FieldResourceID + " IN (  SELECT " + BaseUserRoleTable.FieldRoleID + "   FROM " + BaseUserRoleTable.TableName + "  WHERE " + BaseUserRoleTable.FieldUserID + " = '" + userID + "'         AND " + BaseUserRoleTable.FieldEnabled + " = 1  UNION  SELECT " + BaseUserTable.FieldRole + "   FROM " + BaseUserTable.TableName + "  WHERE " + BaseUserTable.FieldID + " = '" + userID + "')) )  ORDER BY " + BasePermissionTable.FieldSortCode;
            DataTable dataTable = new DataTable(BasePermissionTable.TableName);
            dbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public bool PermissionExists(string permissionID, string resourceCategory, string resourceID)
        {
            bool flag = true;
            string[] names = new string[3];
            string[] values = new string[3];
            names[0] = BaseResourcePermissionTable.FieldResourceCategory;
            values[0] = resourceCategory;
            names[1] = BaseResourcePermissionTable.FieldResourceID;
            values[1] = resourceID;
            names[2] = BaseResourcePermissionTable.FieldPermissionID;
            values[2] = permissionID;
            if (!this.Exists(names, values))
            {
                flag = false;
            }
            return flag;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseResourcePermissionEntity resourcePermissionEntity)
        {
            sqlBuilder.SetValue(BaseResourcePermissionTable.FieldResourceID, resourcePermissionEntity.ResourceID);
            sqlBuilder.SetValue(BaseResourcePermissionTable.FieldResourceCategory, resourcePermissionEntity.ResourceCategory);
            sqlBuilder.SetValue(BaseResourcePermissionTable.FieldPermissionID, resourcePermissionEntity.PermissionID);
            sqlBuilder.SetValue(BaseResourcePermissionTable.FieldDescription, resourcePermissionEntity.Description);
            sqlBuilder.SetValue(BaseResourcePermissionTable.FieldEnabled, resourcePermissionEntity.Enabled ? 1 : 0);
        }

        public static BaseResourcePermissionDao Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new BaseResourcePermissionDao();
                        }
                    }
                }
                return instance;
            }
        }
    }
}